import { ElNotification, ElMessageBox, ElMessage } from 'element-plus'
import nprogress from "nprogress"
import axios from 'axios'

export function notification(message, type = 'success', dangerouslyUseHTMLString = false) {
    ElNotification({
        message,
        type,
        dangerouslyUseHTMLString,
        duration: 3000
    })
}

export function showModel(content = '提示内容', type = 'warning', title = '') {
    return ElMessageBox.confirm(
        content,
        title,
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type,
        }
    )
}

export function showMessage(message = '提示内容', type = 'success', customClass = '') {
    return ElMessage({
        type: type,
        message,
        customClass,
    })
}

export function showPageLoading() {
    nprogress.start()
}

export function hidePageLoading() {
    nprogress.done()
}

const API_BASE_URL = '/user/archives'

export const listArchives = (params) => {
    return axios.get(`${API_BASE_URL}/list`, { params })
}

export const deleteArchive = (id) => {
    return axios.delete(`${API_BASE_URL}/delete/${id}`)
}

export const getArchiveDetail = (data) => {
    return axios.post(`${API_BASE_URL}/detail`, data)
}

/**
 * 格式化日期为相对时间或标准格式
 * @param {string|Date} dateStr - 需要格式化的日期
 * @returns {string} - 格式化后的日期字符串
 */
export function formatDate(dateStr) {
  if (!dateStr) return '';
  
  // 解析字符串为日期对象，注意后端使用GMT+8时区
  const date = new Date(dateStr);
  const now = new Date();
  
  // 确保日期有效
  if (isNaN(date.getTime())) return '';
  
  const diff = now - date;
  
  // 小于1分钟
  if (diff < 60 * 1000) {
    return '刚刚';
  }
  
  // 小于1小时
  if (diff < 60 * 60 * 1000) {
    return `${Math.floor(diff / (60 * 1000))}分钟前`;
  }
  
  // 小于1天
  if (diff < 24 * 60 * 60 * 1000) {
    return `${Math.floor(diff / (60 * 60 * 1000))}小时前`;
  }
  
  // 小于1个月
  if (diff < 30 * 24 * 60 * 60 * 1000) {
    return `${Math.floor(diff / (24 * 60 * 60 * 1000))}天前`;
  }
  
  // 使用toLocaleString方法，确保使用本地时区
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false
  });
}